//
//  ZLAdministratorViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/11.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLAdministratorViewController.h"

#import "ZLAdministratorView.h"
#import "AddressPickerView.h"
#import "ZLAdministratorModel.h"


@interface ZLAdministratorViewController ()

@property (nonatomic, strong) ZLAdministratorView *adminView;

@property (nonatomic, copy) NSString *division_id;
@property (nonatomic, strong) AddressPickerView *pickerView;

@end

@implementation ZLAdministratorViewController

#pragma mark - 点击事件
- (void)rightBarButtonPressed:(id)sender {
#pragma mark 退出登录
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否退出管理员登录" message:@"点击确定自动退出" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:^{
            [[ZLInfoManager sharedManager] removeAdminAccessToken];
        }];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

    [alert addAction:action1];
    [alert addAction:action2];

    [self presentViewController:alert animated:YES completion:nil];
}

- (void)subviewCallBack {
    
    __weak typeof(self) weakSelf = self;
    
    _adminView.addressAction = ^(UIButton *addressBtn) {
        kCX_LOG(@"选择地址");
        
        [weakSelf.pickerView showPicker];
        
        weakSelf.pickerView.enterCallBack = ^(ZLAdministratorModel *model) {
            [addressBtn setTitle:model.name forState:UIControlStateNormal];
            _division_id = model.x_id;
        };
        
    };
    
    _adminView.activeAction = ^{
        kCX_LOG(@"激活赛区");
        
        if (weakSelf.division_id) {
            
            NSString *access_token = [ZLInfoManager sharedManager].adminAccessToken;
            NSDictionary *param = @{@"division_id":weakSelf.division_id,
                                    @"access_token":access_token};
            
            [[YCRequestDataService sharedManager] Post:@"user/user/activation_division" parameters:param success:^(id response) {
                if (response[@"error_code"]) {
                    NSLog(@"%@", response[@"error_msg"]);
                } else {
                    NSLog(@"激活成功");
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"赛区激活成功" message:@"点击确定自动退出" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        [weakSelf dismissViewControllerAnimated:YES completion:^{
                            [[ZLInfoManager sharedManager] removeAdminAccessToken];
                        }];
                    }];
                    [alert addAction:action];
                    [weakSelf presentViewController:alert animated:YES completion:nil];
                }
            } failure:^{
                NSLog(@"网络或服务器出错");
            }];
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择要激活的赛区" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:action];
            [weakSelf presentViewController:alert animated:YES completion:nil];
        }
    };
}

#pragma mark - pickview delegate


#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"管理员界面";
    [self showRightButtonWithTitle:@"退出登录"];
    [self changeRightBarButtonItemTextColor:[UIColor whiteColor]];
  
    [self.view addSubview:self.adminView];

    [_adminView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    [self.view addSubview:self.pickerView];
    
    [self subviewCallBack];
    
}


#pragma mark - 子视图
- (ZLAdministratorView *)adminView {
    if (!_adminView) {
        _adminView = [[ZLAdministratorView alloc] init];
    }
    return _adminView;
}

- (AddressPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[AddressPickerView alloc] initWithFrame:kSCREEN_BOUNDS];
    }
    return _pickerView;
}


#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
